package com.ctvit.sjk.knowledge.dataset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppDatasetMapping;
import com.ctvit.sjk.knowledge.app.service.ITabAppDatasetMappingService;
import com.ctvit.sjk.knowledge.dataset.pojo.domain.TabDataset;
import com.ctvit.sjk.knowledge.dataset.mapper.TabDatasetMapper;
import com.ctvit.sjk.knowledge.dataset.pojo.dto.DatasetQueryDTO;
import com.ctvit.sjk.knowledge.dataset.pojo.dto.DatasetUpdateDTO;
import com.ctvit.sjk.knowledge.dataset.pojo.vo.DatasetGetVO;
import com.ctvit.sjk.knowledge.dataset.pojo.vo.DatasetQueryVO;
import com.ctvit.sjk.knowledge.dataset.service.ITabDatasetService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 知识库Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Service
public class TabDatasetServiceImpl extends ServiceImpl<TabDatasetMapper, TabDataset> implements ITabDatasetService {

    private final ISysUserService userService;

    private final ITabAppDatasetMappingService tabAppDatasetMappingService;

    public TabDatasetServiceImpl(ISysUserService userService, ITabAppDatasetMappingService tabAppDatasetMappingService) {
        this.userService = userService;
        this.tabAppDatasetMappingService = tabAppDatasetMappingService;
    }

    @Override
    public List<DatasetQueryVO> list(DatasetQueryDTO dto) {
        Map<String, String> userMap = new HashMap<>();
        List<DatasetQueryVO> resultList = new ArrayList<>();

        LambdaQueryWrapper<TabDataset> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dto.getDatasetName()), TabDataset::getDatasetName, dto.getDatasetName());
        wrapper.orderByDesc(TabDataset::getUpdateTime);

        List<TabDataset> list = list(wrapper);
        for (TabDataset tabDataset : list) {
            String userName = userMap.get(tabDataset.getCreateId());
            if (StringUtils.isBlank(userName)) {
                SysUser sysUser = userService.selectUserById(Long.parseLong(tabDataset.getCreateId()));
                userName = sysUser.getNickName();
                userMap.put(tabDataset.getCreateId(), userName);
            }

            DatasetQueryVO datasetQueryVO = new DatasetQueryVO();
            datasetQueryVO.setDatasetId(tabDataset.getDatasetId());
            datasetQueryVO.setDatasetName(tabDataset.getDatasetName());
            datasetQueryVO.setDatasetType(tabDataset.getDatasetType());
            datasetQueryVO.setDatasetDesc(tabDataset.getDatasetDesc());
            datasetQueryVO.setPermissionType(tabDataset.getPermissionType());
            datasetQueryVO.setCreateName(StringUtils.defaultString(userName));
            datasetQueryVO.setUpdateTime(tabDataset.getUpdateTime());

            resultList.add(datasetQueryVO);
        }

        return resultList;
    }

    @Override
    public DatasetGetVO get(String datasetId) {
        TabDataset entity = getById(datasetId);

        LambdaQueryWrapper<TabAppDatasetMapping> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(TabAppDatasetMapping::getAppId);
        wrapper.eq(TabAppDatasetMapping::getDatasetId, datasetId);
        List<String> appIdList = tabAppDatasetMappingService.list(wrapper).stream().map(TabAppDatasetMapping::getAppId).collect(Collectors.toList());

        return new DatasetGetVO(entity, appIdList);
    }

    @Override
    public boolean update(String datasetId, DatasetUpdateDTO dto, String userId) {
        LambdaUpdateWrapper<TabDataset> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TabDataset::getDatasetName, dto.getName());
        wrapper.set(TabDataset::getDatasetDesc, dto.getDesc());
        wrapper.set(TabDataset::getEmbeddingModelId, dto.getEmbeddingModelId());
        wrapper.set(TabDataset::getUpdateId, userId);
        wrapper.set(TabDataset::getUpdateTime, LocalDateTime.now());
        wrapper.eq(TabDataset::getDatasetId, datasetId);

        return update(wrapper);
    }

}
